package ntd.util.frame.action.ai;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import ntd.action.ActionAbs;
import ntd.action.bo.ActionInfo;
import ntd.action.bo.ActionOutBo;
import ntd.util.config.common.bo.RequestInfo;
import ntd.util.config.common.exception.ComException;
import ntd.util.frame.data.factory.FactoryData;
import ntd.util.frame.data.tool.ToolMysql;

import java.sql.SQLException;
import java.util.List;

public class ActionAiIotDeviceSelect extends ActionAbs {

    @Override
    public ActionInfo info() {
        return new ActionInfo("Ai设备配置查询", "ai-iot-device-select", new ActionAiIotDeviceSelect());
    }

    @Override
    public ActionOutBo handle() {
        //数据库
        ToolMysql toolMysql = new ToolMysql(FactoryData.dataSource(actionInBo.dsId()));
        Db db = toolMysql.db;

        try {
            //模型类型
            String model = actionInBo.getParamStr("model");

//            ComOpt<Object> comOpt = new ComOpt<>(FactoryData.dataSource(), "things_device_config");
//
//            ComOptQueryWrapper comOptQueryWrapper = new ComOptQueryWrapper("things_device_config")
//                    .eq("flag", "0")
//                    .cusLike("config", StrUtil.format("%ai-vision%open%true%{}",
//                            ObjectUtil.defaultIfBlank(model, "")));
//
//            List<Entity> deviceList = comOpt.list(comOptQueryWrapper);

            String sql = "select id,code,config from things_device_config WHERE flag = 0 AND config LIKE '%ai-vision%open%true%{}'";

            //判断模型类型是否为空
            sql = StrUtil.format(sql,StrUtil.isNotBlank(model) ? model+"%":"");

            List<Entity> deviceList = db.query(sql);

            return new ActionOutBo(deviceList);
        } catch (SQLException e) {
            StaticLog.error(e, "[AI][设备配置][{}]失败...");
            throw new ComException(e.getMessage());
        }
    }
}
